<?php
class Server {
  public static function submit_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if (isset($req->id) && $req->id != 0) {
      $orm->update('pb_page', $req);
    } else {
      $opts = new stdClass();
      $opts->cd = 't.sys_id = :v1';
      $opts->cdv = array(':v1' => $req->sys_id);
      $opts->sel = 't.order';
      $pages = $orm->get('pb_page', 't.order', 'DESC', 1, 1, $opts)->d;
      if (count($pages) > 0) {
        $req->order = $pages[0]->order + 1;
      }
      
      $orm->add('pb_page', $req);
      $req->id = $orm->last_id();
    }
    Server::__clear();
    $opts = new stdClass();
    $opts->cd = 't.sys_id = :v1';
    $opts->cdv = array(':v1' => SYS_ID);
    $req->__objs = $orm->get('pb_page', NULL, NULL, NULL, NULL, $opts)->d;
    echo json_encode($req);
  }
  
  public static function get_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $res = $orm->get('pb_page', NULL, NULL, NULL, NULL, $opts)->d;
    echo json_encode($res);
  }
  
  public static function __clear() {
    if (isset($_SESSION[SYS . 'index'])) {
      unset($_SESSION[SYS . 'index']);
    }
  }
}
?>